//
//  SPBaseMedicalFacialSearchVC.m
//  ZXHT
//
//  Created by Bee on 2019/8/26.
//  Copyright © 2019 zxht. All rights reserved.
//

#import "SPBaseMedicalFacialSearchVC.h"
#import "SPMacros.h"
#import <SPUIKitCategory/SPUIKitCategory.h>

@interface SPBaseMedicalFacialSearchVC ()
@end

@implementation SPBaseMedicalFacialSearchVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    self.navigationItem.titleView = [
    [self setBarButtonItem];
}

- (CGFloat)searchBarWidth {
    return SCREEN_WIDTH - 120;
}

- (void)setBarButtonItem
{
    //    //隐藏导航栏上的返回按钮
    //    [self.navigationItem setHidesBackButton:YES];
    //用来放searchBar的View
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, [self searchBarWidth] + 20, 30)];
    titleView.backgroundColor = [UIColor clearColor];
    //创建searchBar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [self searchBarWidth], 30)];
    self.searchBar = searchBar;
    searchBar.placeholder = _placeholder ?: @"请输入";
    self.searchBar = searchBar;
    
    searchBar.layer.cornerRadius = 15;
    
    searchBar.layer.masksToBounds = YES;
    
    //设置背景图是为了去掉上下黑线
    
    searchBar.backgroundImage = [[UIImage alloc] init];
    
    //searchBar.backgroundImage = [UIImage imageNamed:@"sexBankgroundImage"];
    
    // 设置SearchBar的主题颜色
    searchBar.tintColor = ColorFromHex(0x595959);
    searchBar.barTintColor = [UIColor redColor];
    
    //设置背景色
    
    searchBar.backgroundColor = [UIColor whiteColor];
    
    //    [searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]];
    //    [searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarPosition:(UIBarPositionAny) barMetrics:(UIBarMetricsDefault)];
    
    // 修改cancel
    
    searchBar.showsCancelButton=NO;
    
    searchBar.barStyle=UIBarStyleDefault;
    
    searchBar.keyboardType=UIKeyboardTypeNamePhonePad;
    
    //searchBar.searchBarStyle = UISearchBarStyleMinimal;//没有背影，透明样式
    
    searchBar.delegate=self;
    
    
    
    // 修改cancel
    
    searchBar.showsSearchResultsButton=NO;
    
    //5. 设置搜索Icon
    
    //    [searchBar setImage:[UIImage imageNamed:@"Search_Icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    
    
    
    /*这段代码有个特别的地方就是通过KVC获得到UISearchBar的私有变量
     
      searchField（类型为UITextField），设置SearchBar的边框颜色和圆角实际上也就变成了设置searchField的边框颜色和圆角，你可以试试直接设置SearchBar.layer.borderColor和cornerRadius，会发现这样做是有问题的。*/
    
    UITextField *searchField = nil;
    //一下代码为修改placeholder字体的颜色和大小
    if (@available(iOS 13, *)) {
        searchField = _searchBar.searchTextField;
    } else {
        searchField = [_searchBar valueForKey:@"_searchField"];
        if (!searchField) {
            searchField = [_searchBar valueForKey:@"searchField"];
        }
    }
    
    //2. 设置圆角和边框颜色
    
    if(searchField) {
        
        [searchField setBackgroundColor:[UIColor clearColor]];
        
        //        searchField.layer.borderColor = [UIColor colorWithRed:49/255.0f green:193/255.0f blue:123/255.0f alpha:1].CGColor;
        //
        //        [searchField setBackgroundColor:[UIColor clearColor]];
        //
        //        searchField.layer.borderColor = [UIColor colorWithRed:49/255.0f green:193/255.0f blue:123/255.0f alpha:1].CGColor;
        //
        //        searchField.layer.borderWidth = 1;
        
        searchField.layer.cornerRadius = 23.0f;
        
        searchField.layer.masksToBounds = YES;
        
        // 根据@"_placeholderLabel.textColor" 找到placeholder的字体颜色
        
//        [searchField setValue:[UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
        
    }
    searchBar.returnKeyType = UIReturnKeyDone;
    // 输入文本颜色
    
    searchField.textColor= [UIColor colorWithHexString:@"333333"];
    
    searchField.font= [UIFont systemFontOfSize:15];
    
    // 默认文本大小
    
//    [searchField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
    
    //只有编辑时出现出现那个叉叉
    
    searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [titleView addSubview:searchBar];
    self.navigationItem.titleView = titleView;
}

//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    if (!_searchBar.isFirstResponder) {
//        [self.searchBar becomeFirstResponder];
//    }
//}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchBar resignFirstResponder];
}

#pragma mark - UISearchBarDelegate
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//    searchBar.showsCancelButton = YES;
//}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    //    searchBar.showsCancelButton = YES;
    [self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //    NSString *inputStr = searchText;
    
}

@end

